package com.agileai.pm.module.base.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.bizmoduler.core.StandardServiceImpl;
import com.agileai.pm.cxmodule.ProjectInfoManage;
import com.agileai.pm.domain.Project;
import com.agileai.util.ListUtil;
import com.agileai.util.StringUtil;

public class ProjectInfoManageImpl
        extends StandardServiceImpl
        implements ProjectInfoManage {
    public ProjectInfoManageImpl() {
        super();
    }

	@Override
	public void insertFileManage(DataParam param) {
		String statementId = sqlNameSpace+"."+"insertFileManage";
		this.daoHelper.insertRecord(statementId, param);
	}
	
	public void createRecord(DataParam param) {
		String statementId = sqlNameSpace+"."+"insertRecord";
		processDataType(param, tableName);
		this.daoHelper.insertRecord(statementId, param);
	}

	@Override
	public List<DataRow> projectSelect(String userId) {
		String statementId = sqlNameSpace+"."+"findProjectSelect";
		DataParam param=new DataParam();
		param.put("userId", userId);
		List<DataRow> result = this.daoHelper.queryRecords(statementId,param);
		return result;
	}
	@Override
	public List<Project> queryProjectList(String userId) {
		List<Project> result = new ArrayList<Project>();
		String statementId = sqlNameSpace+"."+"findUserProjects";
		DataParam param = new DataParam("USER_ID",userId);
		List<DataRow> projectRecords = this.daoHelper.queryRecords(statementId, param);
		
		statementId = sqlNameSpace+"."+"findUserProjectActiveTests";
		HashMap<String,DataRow> testMapRecords = this.daoHelper.queryRecords("PI_ID",statementId, param);
		
		for (int i=0;i < projectRecords.size();i++){
			DataRow row = projectRecords.get(i);
			Project project = new Project();
			String projectId = row.getString("PI_ID");
			project.setProjectId(projectId);
			project.setProjectName(row.getString("PI_NAME"));
			project.setProjectCode(row.getString("PI_CODE"));
			DataRow testRow = testMapRecords.get(projectId);
			if (testRow != null){
				String testId = testRow.getString("PT_ID");
				String testName = testRow.getString("PT_NAME");
				project.setActiveTestId(testId);
				project.setActiveTestName(testName);
			}
			result.add(project);
		}
		return result;
	}

	@Override
	public void insertPlanInfo(DataParam param) {
		String statementId = sqlNameSpace+"."+"insertPlanInfo";
		this.daoHelper.insertRecord(statementId, param);
	}

	@Override
	public void insertPeopleInfo(DataParam param) {
		String statementId = sqlNameSpace+"."+"insertPeopleInfo";
		this.daoHelper.insertRecord(statementId, param);
	}

	@Override
	public int getMaxSort(DataParam param) {
		String statementId = sqlNameSpace+"."+"getMaxSort";
    	DataRow result = this.daoHelper.getRecord(statementId, param);
    	int num = result.getInt("PP_SORT");
    	String sort = String.valueOf(num);
    	if(StringUtil.isNullOrEmpty(sort)){
    		num = 1;
    	}else{
    		num++;
    	}
		return num;
	}

	@Override
	public void updateConfigInfo(DataParam emailconfigParam) {
		String statementId = sqlNameSpace+"."+"updateConfigInfo";
		this.daoHelper.updateRecord(statementId, emailconfigParam);
		
	}

	public List<DataRow> findUserType(String userId,String piManager) {
		String statementId = sqlNameSpace+"."+"findUserType";
		DataParam param=new DataParam("userId", userId,"piManager", piManager);
		List<DataRow> result = this.daoHelper.queryRecords(statementId,param);
		return result;
	}
	
	public void updateUserType(DataParam param) {
		String statementId = sqlNameSpace+"."+"updateUserType";
		this.daoHelper.insertRecord(statementId, param);
	}

	@Override
	public DataRow getUserInfo(String userId) {
		String statementId = sqlNameSpace+"."+"getUserInfo";
		DataParam param = new DataParam("userId",userId);
		DataRow result = this.daoHelper.getRecord(statementId, param);
		return result;
	}

	@Override
	public DataRow getActiveTest(String piId) {
		String statementId = sqlNameSpace+"."+"getActiveTest";
		DataRow result = this.daoHelper.getRecord(statementId, new DataParam("PI_ID",piId));
		return result;
	}

	@Override
	public List<DataRow> findMobileRecords(DataParam param) {
		String statementId = sqlNameSpace+"."+"findMobileRecords";
		List<DataRow> result = this.daoHelper.queryRecords(statementId,param);
		return result;
	}
	@Override
	public String getProjectNumRecord(){
		String statementId = sqlNameSpace+"."+"getProjectNumRecord";
		DataParam param = new DataParam();
		DataRow row = this.daoHelper.getRecord(statementId,param);
		statementId = sqlNameSpace+"."+"findRecords";
		List<DataRow> list = this.daoHelper.queryRecords(statementId,param);
		Long num = (Long) row.get("PJ_NUM");
		if(!ListUtil.isNullOrEmpty(list)){
			DataRow recordRow = list.get(list.size()-1);
			String piCode = recordRow.getString("PI_CODE");
			if(!StringUtil.isNullOrEmpty(piCode)){
				Long codeNum = Long.parseLong(piCode);
				if(codeNum==num){
					num++;
				}else if(codeNum>num){
					num = codeNum+1;
				}
			}
		}
		StringBuffer strNum = new StringBuffer();
		for(int i=0;i<4;i++){
			strNum.append("0");
		}
		strNum.append(num);
		String value = strNum.substring(strNum.length()-4,strNum.length());
		return value;
	}
}
